package com.github.permissiondog.cancanpbackend.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class CORSFilter extends BaseFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        res.setHeader("Access-Control-Allow-Origin", "*");
        if (req.getMethod().equals("OPTIONS")) {
            res.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,HEAD,PUT,PATCH");
            res.setHeader("Access-Control-Allow-Headers", "Content-Type,Accept,Authorization");
            res.setStatus(204);
            return;
        }
        chain.doFilter(req, res);
    }
}
